package com.example.shopdemo.service;

import com.example.shopdemo.entity.ShopCategory;
import com.example.shopdemo.repository.ShopCategoryRepository;
import com.example.shopdemo.utils.SpecificationsBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2018-01-19
 **/
@Service
public class ShopCategoryServiceImpl {

    @Autowired
    private ShopCategoryRepository shopCategoryRepository;


    public List<ShopCategory> queryFirstLevelShopCategory(){
        return shopCategoryRepository.findByParentId(new Long(0));
    }


    public List<ShopCategory> queryShopCategory(ShopCategory shopCategoryCondition) {
        Long parentCategoryId = shopCategoryCondition.getParentId();
        SpecificationsBuilder<ShopCategory> specificationsBuilder = new SpecificationsBuilder<>();
        if(!StringUtils.isEmpty(parentCategoryId)){
            specificationsBuilder.with("parentId","=",parentCategoryId);
        }
        Specification<ShopCategory> spec = specificationsBuilder.build();
        Sort.Order order = new Sort.Order(Sort.Direction.DESC,"priority");
        Sort sort = new Sort(order);
        List<ShopCategory> shopCategoryList = shopCategoryRepository.findAll(spec,sort);
        return shopCategoryList;
    }



}
